
    /**  
    * @Title: IpAdrressUtil.java
    * @Package com.efast.cafe.framework.util
    * @Description: TODO(用一句话描述该文件做什么)
    * @author Administrator
    * @date 2018年8月30日
    * @version V1.0  
    */
    
package com.efast.cafe.util;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.bouncycastle.util.IPAddress;

import com.efast.cafe.util.StringUtil;

/**
    * @ClassName: IpAdrressUtil
    * @Description: TODO(这里用一句话描述这个类的作用)
    * @author Administrator
    * @date 2018年8月30日
    *
    */

public class IpAddressUtil {
	/**
     * 获取Ip地址
     * @param request
     * @return
     */
    public static String getIpAdrress(HttpServletRequest request) {
        String Xip = request.getHeader("X-Real-IP");
        String XFor = request.getHeader("X-Forwarded-For");
        if(StringUtil.isNotBlank(XFor) && !"unKnown".equalsIgnoreCase(XFor)){
            //多次反向代理后会有多个ip值，第一个ip才是真实ip
            int index = XFor.indexOf(",");
            if(index != -1){
                return XFor.substring(0,index);
            }else{
                return XFor;
            }
        }
        XFor = Xip;
        if(StringUtil.isNotBlank(XFor) && !"unKnown".equalsIgnoreCase(XFor)){
            return XFor;
        }
        if (StringUtil.isNotBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
            XFor = request.getHeader("Proxy-Client-IP");
        }
        if (StringUtil.isNotBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
            XFor = request.getHeader("WL-Proxy-Client-IP");
        }
        if (StringUtil.isNotBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
            XFor = request.getHeader("HTTP_CLIENT_IP");
        }
        if (StringUtil.isNotBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
            XFor = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (StringUtil.isNotBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
            XFor = request.getRemoteAddr();
        }
        return XFor;
    }
    
    
    /**
    * 此方法描述的是：获得服务器的IP地址
    */
    public static String getLocalIP() throws Exception{
    String sIP = "";
    InetAddress ip = null;
    boolean bFindIP = false;
    Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface
    .getNetworkInterfaces();
    while (netInterfaces.hasMoreElements()) {
    if (bFindIP) {
    break;
    }
    NetworkInterface ni = (NetworkInterface) netInterfaces
    .nextElement();
    Enumeration<InetAddress> ips = ni.getInetAddresses();
    while (ips.hasMoreElements()) {
    ip = (InetAddress) ips.nextElement();
    if (!ip.isLoopbackAddress()
    && ip.getHostAddress().matches(
    "(\\d{1,3}\\.){3}\\d{1,3}")) {
    bFindIP = true;
    break;
    }
    }
    }
    if (null != ip) {
    sIP = ip.getHostAddress();
    }
    return sIP;
    }
    
    /**
    * 此方法描述的是：获得服务器的IP地址(多网卡)
    */
    public static List<String> getLocalIPS() throws Exception{
	    InetAddress ip = null;
	    List<String> ipList = new ArrayList<String>();
	    Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface
	    .getNetworkInterfaces();
	    while (netInterfaces.hasMoreElements()) {
	    NetworkInterface ni = (NetworkInterface) netInterfaces
	    .nextElement();
	    Enumeration<InetAddress> ips = ni.getInetAddresses();
	    while (ips.hasMoreElements()) {
	    ip = (InetAddress) ips.nextElement();
	    if (!ip.isLoopbackAddress()
	    		&& ip.getHostAddress().matches(
	    				"(\\d{1,3}\\.){3}\\d{1,3}")) {
	    		ipList.add(ip.getHostAddress());
	    		}
	    	}
	    }
	    
	    return ipList;
    }
    
    
    public static String getMacByIp(String ip) throws SocketException, UnknownHostException{
    	NetworkInterface ne=NetworkInterface.getByInetAddress(InetAddress.getByName(ip));
    	byte[]mac=ne.getHardwareAddress();
    	StringBuffer sb = new StringBuffer("");
    	for(int i=0; i<mac.length; i++) {
    		if(i!=0) {
    			sb.append("-");
    		}
    		//字节转换为整数
    		int temp = mac[i]&0xff;
    		String str = Integer.toHexString(temp);
    		//System.out.println("每8位:"+str);
    		if(str.length()==1) {
    			sb.append("0"+str);
    		}else {
    			sb.append(str);
    		}
    	}
    	return sb.toString().toUpperCase();
    }
    
   public static void main(String[] args) {
	   try {
		System.out.println(IpAddressUtil.getLocalIP());
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
   }
}
